package tk.heimashuma.store.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.heimashuma.store.entity.*;
import tk.heimashuma.store.entity.dto.CarNum;
import tk.heimashuma.store.repository.CarRepository;
import tk.heimashuma.store.repository.TransactionCommodityCarRepository;
import tk.heimashuma.store.repository.TransactionRepository;
import tk.heimashuma.store.service.CarService;

import java.math.BigDecimal;
import java.util.*;

@Service
public class CarServiceImpl implements CarService {

    @Autowired
    private CarRepository carRepository;
    @Autowired
    private TransactionRepository transactionRepository;
    @Autowired
    private TransactionCommodityCarRepository transactionCommodityCarRepository;

    @Override
    public Car saveToCar(Commodity commodity, Users user, Integer num) {

        Car car = carRepository.findAllByUserAndCommodity(user, commodity);
        if(Optional.ofNullable(car).isPresent()){
            car.setCount(car.getCount() + num);
        }else{
            car = new Car();
            car.setCount(num);
        }
        car.setSubtotal(commodity.getPrice().multiply(BigDecimal.valueOf(Long.valueOf(car.getCount()))));
        car.setCommodity(commodity);

        car.setUser(user);
        carRepository.save(car);
        return car;
    }

    @Override
    public List<Car> findAllCarByUser(Users user) {
        List<Car> car = carRepository.findAllByUser(user);
        if(Optional.ofNullable(car).isPresent()){
            for (int i = 0; i < car.size(); i++) {
                car.get(i).setUser(null);
                car.get(i).getCommodity().setImagesList(null);
                car.get(i).getCommodity().setType(null);
            }
        }
        return car;
    }

    @Override
    public CarNum findAllCarAndCheckNumsByIds(Integer[] ids, Integer [] nums) {
        List<Car> carList  = new ArrayList<>();
        BigDecimal count = new BigDecimal(0.00);
        for(int i = 0 ; i < ids.length ;i++){
            Optional<Car> byId = carRepository.findById(ids[i]);
            if(byId.isPresent()){
                Car car = byId.get();
                carList.add(car);
                if(!car.getCount().equals(nums[i])){
                    car.setCount(nums[i]);
                    car.setSubtotal(car.getCommodity().getPrice().multiply(BigDecimal.valueOf(car.getCount())));
                }
                count = count.add(car.getSubtotal());
                carRepository.delete(car);
            }
        }
        CarNum carNum = new CarNum();
        carNum.setCar(carList);
        carNum.setCountNum(ids.length);
        carNum.setCountPrice(count);
        return carNum;
    }

    @Override
    @Transactional
    public String createOrder(CarNum carNum, Users user) {
        Transaction transaction = new Transaction();
        transaction.setTId(UUID.randomUUID().toString().replace("-",""));
        transaction.setState(4);
        transaction.setIphoneNumber(user.getIphone());
        transaction.setTotalMoney(carNum.getCountPrice());
        transaction.setCreateTime(new Date());
        transaction.setUser(user);
        List<Commodity> commodities = new ArrayList<>();
        for(Car car : carNum.getCar()){
            commodities.add(car.getCommodity());
            TransactionCommodityCar tc = new TransactionCommodityCar();
            tc.setSubtotal(car.getSubtotal());
            tc.setCount(car.getCount());
            tc.setTransaction(transaction);
            tc.setCommodity(car.getCommodity());
            transactionCommodityCarRepository.save(tc);
        }
        transaction.setCommodityList(commodities);
        transactionRepository.save(transaction);
        return transaction.getTId();
    }

    @Override
    public Long statisticsCarByUsers( Users user) {
        Long count = carRepository.countByUser(user);
        return count;
    }

    @Override
    public Boolean delCar(Integer cid) {
        Optional<Car> byId = carRepository.findById(cid);
        if(byId.isPresent()){
            carRepository.delete(byId.get());
            return true;
        }
        return  false;
    }


}
